package com.beer.beerfriendfinder;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

public class ImageHelper {
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		final float roundPx = pixels;

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);

		return output;
	}
	
	public static void saveImageToApp(Context context,Bitmap bm, String name) {
		//create a file to write bitmap data
		if(bm==null){
			return;
		}		
		String filename=name;
		File f;
		if(filename.contains(".jpg")){
			f =new File(context.getFilesDir(), filename);
		}else{
			f =new File(context.getFilesDir(), filename+".jpg");	
		}
			
		Log.e("path luu avatar contact",f.getAbsolutePath());
		//write the bytes in file
		try {
			f.createNewFile();
			byte[] bitmapdata=null;
			Bitmap bitmap = bm;
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			bitmap.compress(CompressFormat.PNG, 0, bos);
			bitmapdata = bos.toByteArray();
			FileOutputStream fos;
			fos = new FileOutputStream(f);
			fos.write(bitmapdata);
			fos.close();
		} catch (FileNotFoundException e) {
			Log.e("ImageHelper","LOI LUU FILE,FILE NOT FOUND:"+e.toString());
		}catch(IOException e){
			Log.e("ImageHelper","LOI LUU FILE:"+e.toString());
		}
		
		
	}

	public static Bitmap getBitmapFromURL(String linkanh){
		if(linkanh.trim()=="")
		{
			return null;
		}
		try{
			Log.e("src",linkanh);
			URL url=new URL(linkanh);
			HttpURLConnection conn=(HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream input=conn.getInputStream();
			Bitmap returnBitmap=BitmapFactory.decodeStream(input);
			Log.e("Bitmap","returned");
			return returnBitmap;
		}catch(IOException e){
			e.printStackTrace();
			Log.e("Exception",e.getMessage());
			return null;
		}
	}
	
	public static Bitmap decodeFile(File f, int reqHeight, int reqWidth){
	    try {
	        //Decode image size
	    	 BitmapFactory.Options o = new BitmapFactory.Options();
		        o.inJustDecodeBounds = true;
		        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

		        //Find the correct scale value. It should be the power of 2.
		        int inSampleSize=1;
		        while(o.outWidth/inSampleSize/2>=reqWidth && o.outHeight/inSampleSize/2>=reqHeight)
		        	inSampleSize*=2;

		        //Decode with inSampleSize	        
		        o.inSampleSize=inSampleSize;
		        o.inJustDecodeBounds = false;
		        return BitmapFactory.decodeStream(new FileInputStream(f),null, o);
	    } catch (FileNotFoundException e) {
	    	e.printStackTrace();
	    }

	    return null;
	}
	
	public static void unbindDrawables(View view) {
		if (view.getBackground() != null) {
			view.getBackground().setCallback(null);
		}
		if (view instanceof ViewGroup) {
			for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
				unbindDrawables(((ViewGroup) view).getChildAt(i));
			}
			((ViewGroup) view).removeAllViews();
		}
	}
	
}
